package com.example.administrator.thread_test;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;


public class MainActivity extends Activity {
    private int x =50;
    private TextView tv;
//    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
//        if(msg.what==123)
//        {
//            tv.setText(msg.arg1);
//        }
    }
};

//    final Handler handler = new Handler();
    Runnable r = new Runnable() {
        @Override
        public void run() {
            x++;
            if(x>100)
            {
                handler.removeCallbacks(this);
            }else{
                tv.setText(x);

                handler.postDelayed(this,500);
            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         tv = (TextView)findViewById(R.id.textView);
        Button btn1 = (Button)findViewById(R.id.button1);
        Button btn2 = (Button)findViewById(R.id.button2);
        btn1.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                handler.postDelayed(r,2000);
//                                while (x<100) {
////                                    Log.d("test", "x=" + x++);
//                                    x++;
////                                    Message m = new Message();
////                                    m.what = 123;
////                                    m.arg1 = x;
////                                    handler.sendMessage(m);
//                                    handler.post(new Runnable() {
//                                        @Override
//                                        public void run() {
//                                            tv.setText(x);
//                                        }
//                                    });
//                                    try {
//                                        Thread.sleep(500); //休息500ms继续
//                                    } catch (InterruptedException e) {
//                                        e.printStackTrace();
//                                    }
//                                }

                            }
                        }).start();
                    }
                }
        );
        btn2.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        handler.removeCallbacks(r,2000);
                    }
                }
        );
    }
}
